$(function () {
    $("#jqGrid").jqGrid({
        url: '../../user/list',
        datatype: "json",
        colModel: [
            { label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true,hidden:true},
            { label: '用户名', name: 'username', index: 'username', width: 60 },
            { label: '真实姓名', name: 'realname', index: 'realname', width: 50 },
            { label: '昵称', name: 'nickname', index: 'nickname', width: 50 },
            { label: '手机号', name: 'phone', index: 'phone', width: 60 },
            { label: '性别', name: 'sex', index: 'sex', width: 50 ,
                formatter:function(v){
                    if(v==0){
                        return "女";
                    }else if(v==1){
                        return "男";
                    }else{
                    	return "";
					}
                }
			},
            { label: '密码', name: 'password', index: 'password', width: 80,hidden:true },
            { label: '邮箱', name: 'email', index: 'email', width: 80,hidden:true },
            { label: '头像路径', name: 'avatar', index: 'avatar', width: 120 },
            { label: '状态', name: 'state', index: 'state', width: 60 ,
                formatter:function(v){
                    if(v==0){
                        return "禁用";
                    }else if(v==1){
                        return "正常";
                    }else{
                        return "";
                    }
                }
			},
            { label: '创建时间', name: 'createTime', index: 'create_time', width: 130},
            { label: '极光注册ID', name: 'registrationId', index: 'registration_id', width: 100 }
        ],
		viewrecords: true,
        height: 700,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		user: {},
        q:{
            realname:null,
            phone:null
        },
        imageUrl: ''

	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.user = {};
		},
		update: function (event) {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(userId)
		},
		saveOrUpdate: function (event) {
			var url = vm.user.userId == null ? "../../user/save" : "../../user/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.user),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        becomeDriver: function (event) {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }
            confirm('确定选中的记录成为司机？', function(){
                $.ajax({
                    type: "POST",
                    url: "../../user/becomeDriver",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
		del: function (event) {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../../user/delete",
				    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(userId){
			$.get("../../user/info/"+userId, function(r){
                vm.user = r.user;
                var frontUrl = '/qwj_shop/fileDirect/backManageDriver/';
                if(vm.user.avatar == null){
                    vm.imageUrl = "../../swagger/noPicture.jpg"
                }else {
                    vm.imageUrl = frontUrl + vm.user.avatar;
                }
            });
		},
        handleAvatarSuccess: function(res, file) {
            vm.user.avatar = res.url;
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload: function(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                alert('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                alert('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'realname': vm.q.realname,'phone':vm.q.phone},
                page:page
            }).trigger("reloadGrid");
		}
	}
});